package com.feijia.aijingjingus;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.feijia.aijingjingus.util.PreferencesUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import retrofit2.Retrofit;
import static com.feijia.aijingjingus.GlobalHelper.GUserNum;
import static com.feijia.aijingjingus.GlobalHelper.md5;

public class MainlLoginActivity extends AppCompatActivity {
    String toMail;
    EditText et_mail;
    Button bt_getCode;
    Handler handler = new Handler();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainl_login);
        et_mail=(EditText)findViewById(R.id.et_mail);
        bt_getCode=(Button)findViewById(R.id.bt_getCode);
    }

    public void onClickBack(View view) {
        Intent intent = new Intent("signUp_Activity");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    public void getCode(View view) {
        toMail=et_mail.getText().toString();
                // 判断邮箱格式是否正确
                if(isValidEmail(toMail)) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Login(toMail,md5("111"));
                        }
                    }).start();

                }else {
                    Toast.makeText(this, getString(R.string.Mailbox_format_error), Toast.LENGTH_SHORT).show();
                }
    }



    /**
     * 验证是否为正确的邮箱号
     *
     * @param email
     * @return
     */
    public static boolean isValidEmail(String email) {
        // 1、\\w+表示@之前至少要输入一个匹配字母或数字或下划线 \\w 单词字符：[a-zA-Z_0-9]
        // 2、(\\w+\\.)表示域名. 如新浪邮箱域名是sina.com.cn
        // {1,3}表示可以出现一次或两次或者三次.
        String reg = "\\w+@(\\w+\\.){1,3}\\w+";
        Pattern pattern = Pattern.compile(reg);
        boolean flag = false;
        if (email != null) {
            Matcher matcher = pattern.matcher(email);
            flag = matcher.matches();
        }
        return flag;
    }

    private  void Login(String PhoneNum, String Password){
        JSONObject root= new JSONObject();
        try {
            root.put("UserNo",  PhoneNum);
            root.put("UserPassword", Password);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        final JSONObject out = GlobalHelper.PostData(root,"http://120.77.255.57/ting/nopslogin.php");
        try {
            if (out.getInt("OperateResult")==1) {
                String OperateMsg = out.getString("OperateMsg"); // 获取操作结果信息
                System.out.printf(" OperateResult : %s \n OperateMsg : %s\n", out.getInt("OperateResult"), OperateMsg);
                GUserNum =PhoneNum;
                PreferencesUtils.putString(getApplicationContext(),"USER_NAME", PhoneNum);
                Intent intent =new Intent("Mail_codeActivity");
                startActivity(intent);
            } else {
                System.out.println("predict error");
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), getString(R.string.LoginFailed), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (JSONException e) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
                }
            });
            e.printStackTrace();
        }
    }

}
